// 主界面
;define(function(require, exports, module) {
    'use strict';
    var util = require("util");
    var base = require("base");
    var leftMenu = require("../leftMenu/leftMenu");
    var topMenu = require("../topMenu/topMenu");
    var router = require("../hash/pageRouter");
    var footer = require("../footer/footer_main");
    var dialog = require("dialog");
    var validate = require("validate");
    var constant = require("constant");
    var databind = require("databind");
    var html_financingDetail_recieveNotice = require("./financingDetail_recieveNotice.html");
    var pageIndex = 1,
        pageSize = 10,
        totalPageCount = 1,
        totalRecordCount = 0; //分页初始化
    var imgSrc;
    var noticeState;
    var init = function(){
    	
    	
    	footer.init();
		$("#footer").removeClass("none");
		leftMenu.init(5);
		topMenu.init(1);
		$(".footer").removeClass("none");
		$(".table-section").removeClass("none");
		$("#leftMenu6").find(".leftMenu-item").removeClass("leftMenu-active");
		$("#leftMenu6 .leftMenu li:first-child").addClass("leftMenu-open");
		$(".recieveNoticeList").addClass("leftMenu-active");
    	inquireRecieveNoticePageList();
		initEvent();
		router.init();
    };
	var initEvent = function(){
		//若未登录，则返回登陆页面
		if(base.getData("sid")==null||base.getData("sid")==""||base.getData("sid")==undefined){
			top.location.href="../login/login.html";
		}
		
		$("#loanNoticeSearch").on("click",function(){
			pageIndex = 1;
			pageSize = 10;
			totalPageCount = 1;
			totalRecordCount = 0; //分页初始化
			inquireRecieveNoticePageList();
		});
    };
    
    //查询收货通知列表
    var inquireRecieveNoticePageList = function(){
    	//var body = base.parseForm($(".page-content-search"));
    	var productType=$(".wrapper select[name=productType]").val();
    	noticeState = $(".noticeState").val();
    	var financeNumber = $("#financeNumber").val();
    	var bulkstockName = $("#bulkstockName").val();
    	base.fnAjax({
            action: "inquireRecieveNoticePageList.json",
            oData: {
            	pageIndex: pageIndex,
                pageSize: pageSize,
                noticeState:noticeState,
                financeNumber:financeNumber,
                enterpriseName:$("#enterpriseName").val(),
                bulkstockName:bulkstockName,
                productType: productType
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                	juicer.register("getValueByKey", constant.getValueByKey);
                	var tpl = $("#logisticsCompanyListTpl").html();
                    var html = juicer(tpl, data);
                    $("#newTaskListBody").html(html);
                    totalPageCount = data.totalPageCount;
                    totalRecordCount = data.totalRecordCount;
                    base.pageControl(
                    	$("#pagenation"),
                        totalRecordCount,
                        data.pageIndex,
                        data.pageSize,
                        function(p) {
                            pageIndex = p;
                            inquireRecieveNoticePageList();
                    });
                    //详情
                    $(".handle-type").unbind("click").click(function(){
						var recieveNoticeId = [];
	           			var $this = $(this).parents("tr.tr_style");
			  			recieveNoticeId.push($this.attr("recieveNoticeId"));
			   			router.route("financing/recieveNoticeDetail.html?recieveNoticeId="+recieveNoticeId+"&noticeState="+noticeState,true);
					});
					
					//修改收货通知
					$(".update").unbind("click").click(function(){
						inquireRecieveNoticeDetail($(this).parents("tr").attr("financeId"), $(this).parents("tr").attr("recieveNoticeId"));
					});
					
                } else {
                    base.alert(2,"信息获取失败");
                }
            }
        });
    };
    
    var loadLogisticsCompanyList = function() {
		base.fnAjax({
			action : "inquireLogisticsCompanyListByCompanyName.json",
			async : false,
			oData : {
				pageSize : 50
			}, // 传递参数
			fnSuccess : function(data) { // 成功回调
				if (data.status == 200) {
					var tpl = $("#supervisionTpl").html();
					var html = juicer(tpl, data);
					$("#supervisionCompanyName").html(html);
					
					$("#supervisionCompanyName").on("change", function(){
						$("#recieveNotice_dialog input[name=logisticsCompanyName]").val($("#supervisionCompanyName option:selected").text());
					});
				} else {
					base.alert(3, data.message);
				}
			}
		});
	};
	
	var inquireRecieveNoticeDetail = function(financeId, recieveNoticeId) {
		base.fnAjax({
			action : "inquireRecieveNoticeDetail.json",
			async : false,
			oData : {
				recieveNoticeId : recieveNoticeId
			}, // 传递参数
			fnSuccess : function(data) { // 成功回调
				if (data.status == 200) {
					$("#popdialog").html(html_financingDetail_recieveNotice);
					$("#recieveNotice_dialog .dialogContent").attr("action", "updateRecieveNotice");
					loadLogisticsCompanyList();
					
					//加载货物信息
					var tpl = $("#financingDetail-bulkstock1").html();
					var	html = juicer(tpl, data.datas[0]);
					$("#recieveNotice_dialog .recieveNoticeTable").html(html);
					databind.fillForm($("#recieveNotice_dialog .dialogContent-recieveNotice"), data.datas[0]);
					
					databind.fillForm($("#recieveNotice_dialog"), data.datas[0]);
					dialog.showDialog($("#recieveNotice_dialog"));
					
					$("#recieveNotice_dialog .dialogHandlerBtn").unbind("click").click(function(){
						databind.submitDom($("#recieveNotice_dialog .dialogContent"), function(){
							base.alert(1, "修改成功！");
							dialog.hideDialog($("#recieveNotice_dialog"));
						});
					});
				} else {
					base.alert(3, data.message);
				}
			}
		});
	};
	
    return {
        init:init
    };
});